﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LDSP.BRL;
using LDSP.Util.Constants;
using System.IO;
using System.Web;

namespace LDSP.Util
{
    public class TemplateValidator
    {
        public HttpServerUtility _server;

        public TemplateValidator(HttpServerUtility server)
        {
            _server = server;
        }

        /// <summary>
        /// 校验适用法规模板文件的列名的正确性。
        /// </summary>
        /// <param name="templateFile">模板名的全路径</param>
        /// <returns></returns>
        public IList<string> ValidateFavoriteCodification(string templateFile, TemplateType type)
        {
            string defaultTemplateFile = TemplateFactory.GetDefaultTemplateFileByType(type);

            return Validate(templateFile, _server.MapPath(defaultTemplateFile));
        }

        private IList<string> Validate(string templateFile, string defaultTemplate)
        {
            IList<string> invalidColumns = new List<string>();

            IList<string> defaultColumns = GetColumnsByFile(defaultTemplate);
            IList<string> validateColumns = GetColumnsByFile(templateFile);

            foreach (string col in validateColumns)
            {
                if (!defaultColumns.Contains(col))
                {
                    invalidColumns.Add(col);
                }
            }

            return invalidColumns;
        }

        private IList<string> GetColumnsByFile(string templateFile)
        {
            IList<string> columns = new List<string>();

            ExcelClass cls = new ExcelClass(templateFile);
            DataTable dt = cls.ToDataTable(0);
            foreach (DataColumn col in dt.Columns)
            {
                columns.Add(col.ColumnName);
            }

            return columns;
        }
    }
}
